package org.itec.yeast.trdo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.itec.yeast.db.DataManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TRAlleleSQL {
	private static final Logger log = LoggerFactory.getLogger(TRAlleleSQL.class);
	private static final String SQL_INSERT = "insert into allele("
											+ "GENE_idGENE, Allele_Name, Mutant_Info, Mutant_Type"
											+ ") values (?, ?, ?, ?)";
	
	private Object[] getParams(TRAllele allele){
		Object[] params = new Object[]{
			allele.getGeneID(),
			allele.getAlleleName(),
			allele.getMutantInfo(),
			allele.getMutantType()
		};
		
		return params;
	}
	
	public int getAlleleID(String alleleName){
		String sql = "select idALLELE from allele where Allele_Name = ?";
		Object[] params = new Object[]{alleleName};
		List alleleID = DataManager.executeQuery(sql, params);
		if (alleleID == null || alleleID.size() == 0){
			return -1;
		} else if (alleleID.size() > 1){
			log.error("有重复的Allele存在，AlleleName = " + alleleName);
			return -1;
		}
		Map<String, Integer> result = (Map) alleleID.get(0);
		return result.get("idALLELE");
	}
	
	public void insert(TRAllele allele){
		Object[] params = getParams(allele);
		int result = DataManager.executeUpdate(SQL_INSERT, params);
		if (result != 0){
			log.info("插入YeastGene数据成功");
		} else {
			log.info("未插入YeastGene数据");
		}
	}
	
	public void insert(List<TRAllele> alleles){
		int result = -1;
		List<Object[]> list = new ArrayList<Object[]>();
		long start = System.currentTimeMillis();
		for (int i = 0; i < alleles.size(); i++){
			TRAllele allele = alleles.get(i);
			Object[] params = getParams(allele);
			list.add(params);
		}
		
		result = DataManager.executeBatchInsert(SQL_INSERT, list);
		long end = System.currentTimeMillis();
		log.info("插入" + result + "条YeastGene记录，共用" + (end - start) + "ms");
	}
	
	public boolean isAlleleExist(TRAllele allele){
		boolean exist = false;
		String sql = "select idALLELE from allele where Allele_Name = ? and Mutant_Info = ? and Mutant_Type = ?";
		Object[] params = new Object[]{allele.getAlleleName(), allele.getMutantInfo(), allele.getMutantType()};
		List list = DataManager.executeQuery(sql, params);
		if (list != null && list.size() > 0){
			exist = true;
		}
		
		return exist;
	}
	
	public List<String> getStoredAlleles(){
		List<String> result = new ArrayList<String>();
		List<Map> tmp = new ArrayList<Map>();
		String sql = "select Allele_Name from allele";
		tmp = DataManager.executeQuery(sql);
		if (tmp != null){
			for (int i = 0; i < tmp.size(); i++){
				Map<String, String> name = tmp.get(i);
				result.add(name.get("Allele_Name"));
			}
		}
		
		return result;
	}
	
	public static void main(String[] args){
		TRAlleleSQL sql = new TRAlleleSQL();
		List<String> list = sql.getStoredAlleles();
		for (int i = 0; i < list.size(); i++){
			System.out.println(list.get(i));
		}
		System.out.println(list.size());
	}
}
